package com.homework;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @description:
 * @author: jhy
 * @time: 2021/4/25 20:57
 */
public class FileDemo {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {

        //1.拿到File类的Class类对象
        Class<?> cls = Class.forName("java.io.File");

        //2.得到File类所有的构造器并打印--->getDeclaredConstructors()
        Constructor<?>[] constructors = cls.getDeclaredConstructors();
        for(Constructor constructor : constructors){
            System.out.println("File类的构造器：" + constructor);
        }

        //3.拿到指定的构造器，通过构造器生成File实例对象
        Constructor<?> declaredConstructor = cls.getDeclaredConstructor(String.class);
        File obj = (File)declaredConstructor.newInstance("E:\\newFile.txt");

        //4.通过反射得到creatNewFile()方法
        Method createNewFile = cls.getDeclaredMethod("createNewFile");

        //5.调用createNewFile()方法
        createNewFile.invoke(obj);


    }
}
